package cn.edu.bzu.ie.sf.mealsmisweb.controller;

import cn.edu.bzu.ie.sf.mealsmisweb.common.MyUtils;
import cn.edu.bzu.ie.sf.mealsmisweb.entity.Account;
import cn.edu.bzu.ie.sf.mealsmisweb.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version v1.0.0
 * @Package : cn.edu.bzu.ie.sf.mealsmisweb.controller
 * @Description : 伙食账目公布表控制器
 * @Create on : 2021/8/18 18:23
 * @author: Feng Song 宋锋
 **/
@RestController
@RequestMapping(value="/account")
public class AccountController {
    @Autowired
    private AccountService accountService;    // 业务接口实例

    /**
     * 取得某个月份下的，某的你部门的所有的子部门的伙食账目公布表
     * @param pid 父部门
     * @return List<Account> 集合实例
     */
    @RequestMapping(value = "/getAllAccountListByPidAndMonth")
    public List<Account> getAllAccountListByPidAndMonth(int pid){
        String month = MyUtils.getMonthStrFromNow();
        return accountService.getAllAccountListByPidAndMonth(pid, month);
    }

    /**
     * 获取某个部门下的所有账目公布表分页信息
     * @param depid 部门
     * @param pageNum 页面
     * @param pageSize 每个分页的大小
     * @return List<Accout> 集合实例
     */
    @RequestMapping(value = "/getAccountPageListByDepid")
    public Map<String, Object> getAccountPageListByDepid(int depid, int pageNum, int pageSize){
        Map<String, Object> map = new HashMap<>();
        long count = accountService.getCountByDepid(depid);
        map.put("total", count);
        List<Account> list = accountService.getAccountPageListByDepid(depid, (pageNum-1)*pageSize, pageSize);  // 列表集合数据
        map.put("list", list);
        return map;
    }

    /**
     * 取得某个部门某个月份的账目公布表信息
     * @param depid 部门
     * @return Account 实例
     */
    @RequestMapping(value = "/getAccoutByDepidAndMonth")
    public Account getAccoutByDepidAndMonth(int depid){
        String month = MyUtils.getMonthStrFromNow();
        return accountService.getAccoutByDepidAndMonth(depid, month);
    }

    /**
     * 添加账目信息
     * @param item 要添加的账目，无id
     * @return int 影响的行数，有id
     */
    @PostMapping(value="/addAccount")
    public Account addAccount(Account item){
        item.setMonth(MyUtils.getMonthStrFromNow());
        item.setCreate_dt(MyUtils.getDateTimeStrFromNow());
        int iRow = accountService.addAccount(item);
        if(iRow>0){
            return item;
        }else{
            return null;
        }
    }

    /**
     * 修改账目信息
     * @param item 要修改的账目信息
     * @return int 影响的行数
     */
    @PostMapping(value="/editAccount")
    public Account editAccount(Account item){
        item.setMonth(MyUtils.getMonthStrFromNow());
        item.setCreate_dt(MyUtils.getDateTimeStrFromNow());
        int iRow = accountService.editAccount(item);
        if(iRow>0){
            return item;
        }else{
            return null;
        }
    }

    /**
     * 删除账目信息
     * @param id 要删除账目的id
     * @return int 影响的行数
     */
    @PostMapping(value="delAccount")
    public int delAccount(int id){
        return accountService.delAccount(id);
    }

}
